package com.junjie.checkin.handller.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @author 洪浚杰
 * @description
 * @date 2025/6/12 09:32
 */
// 实现MetaObjectHandler接口，编写对应的策略逻辑
@NoArgsConstructor
public class MybatisPlusHandler implements MetaObjectHandler {
    // 插入填充时逻辑
    @Override
    public void insertFill(MetaObject metaObject) {
        /** setFieldValByName（String fieldName，Object fieldVal，MetaObject metaObject）
         fieldName:需要填充的参数名,注意是实体类
         fieldVal：填充的值
         **/
        if (metaObject.hasSetter("avatar")) {
            Object role = getFieldValByName("avatar", metaObject);
            if (role == null) {
                setFieldValByName("avatar","https://cyb-sky-itcast.oss-cn-beijing.aliyuncs.com/af40b895-6f15-424f-bd3d-04dc0618c2c2.webp", metaObject); // 默认值为 0
            }
        }
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);

    }

    // 更新填充时逻辑
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}